package com.mdd.common.util;

import com.mdd.common.entity.yangyangLeadingEnterprise.TAppUser;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.CookieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

public class SessionHelper {
	public static Logger logger = LoggerFactory.getLogger(SessionHelper.class);

	public static final String PLATFORM_USER_TAG="platform_user";//平台PC用户
	public static final String MOBILE_USER_TAG="mobile_user";//app端用户
	public static final String MANAGER_TAG="shop_user";//店铺PC后台用户
	
	//保存app端用户信息
	 public static  void saveMobileUser(HttpServletRequest req,HttpServletResponse rep,TAppUser appUser){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 cookieSession.save(rep,MOBILE_USER_TAG,appUser,"/");
	 }
	 //读取cookie中的appuser用户信息------APP
	 public static TAppUser readMobileUser(HttpServletRequest req,HttpServletResponse rep){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 TAppUser user = cookieSession.read(req,MOBILE_USER_TAG, TAppUser.class);
		 if(user!=null) {
			 return user;
		 }
		 return cookieSession.read(req,MOBILE_USER_TAG, TAppUser.class);
	 }

	//读取cookie中的appuser用户信息
	public static TAppUser readMobileUser(HttpServletRequest req){
		CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		TAppUser user = cookieSession.read(req,MOBILE_USER_TAG, TAppUser.class);
		if(user!=null) {
			return user;
		}
		return cookieSession.read(req,MOBILE_USER_TAG, TAppUser.class);
	}


	 //删除app端用户信息
	 public static void removeMobileUser(HttpServletRequest req,HttpServletResponse rep){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 cookieSession.remove(req,rep, MOBILE_USER_TAG);
	}
	 
	 
	 //保存pc端的商户信息
	 public static void saveShop(HttpServletRequest req,HttpServletResponse rep,TAppUser shop){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 cookieSession.save(rep,MANAGER_TAG,shop,"/");
	 }
	 
	 //读取pc端的商户信息
	 public static TAppUser readShop(HttpServletRequest req,HttpServletResponse rep){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 TAppUser user = cookieSession.read(req,MANAGER_TAG, TAppUser.class);
		 req.setAttribute("user", user);
		 return user;
	 }
	 
	 //删除pc端的商户信息
	 public static void removeShop(HttpServletRequest req,HttpServletResponse rep){
		 CookieSession<TAppUser> cookieSession = new CookieSession<TAppUser>();
		 cookieSession.remove(req,rep, MANAGER_TAG);
		 
	 }

//	 //保存pc端的平台用户信息
//	 public static void savePlatform(HttpServletRequest req,HttpServletResponse rep,PlatformUser platformUser){
//		 CookieSession<PlatformUser> cookieSession = new CookieSession<PlatformUser>();
//		 cookieSession.save(rep,SessionFilter.PLATFORM_USER_TAG,platformUser,"/");
//	 }
//
//	 //读取pc端的平台用户信息
//	 public static PlatformUser readPlatform(HttpServletRequest req,HttpServletResponse rep){
//		 CookieSession<PlatformUser> cookieSession = new CookieSession<PlatformUser>();
//		 return cookieSession.read(req,SessionFilter.PLATFORM_USER_TAG, PlatformUser.class);
//	 }
//
//	 //删除pc端的平台用户信息
//	 public static void removePlatform(HttpServletRequest req,HttpServletResponse rep){
//		 CookieSession<PlatformUser> cookieSession = new CookieSession<PlatformUser>();
//		 cookieSession.remove(req,rep, SessionFilter.PLATFORM_USER_TAG);
//
//	 }

}






